<!--
 * @Author: JiraiyaChen 979526234@qq.com
 * @Date: 2025-03-28 16:57:51
 * @LastEditors: JiraiyaChen 979526234@qq.com
 * @LastEditTime: 2025-07-07 16:56:17
 * @Description: 评标记录 弹框
-->

<template>
  <ClDialog v-bind="$attrs" :title="getTitle" width="800px">
    <ClBar :loading="loaded" bar-height="400px">
      <el-form ref="refForm" :disabled="disabled" label-width="120px" :model="formData" style="max-width: 600px">
        <el-form-item
          v-if="!abandonedBidFlag"
          label="供应商名称"
          prop="supplierIds"
          required
          :rules="{ required: true, message: '请选择供应商名称' }"
        >
          <el-select v-model="formData.supplierIds" placeholder="请选择供应商名称">
            <el-option v-for="item in supplierInfoListRef" :key="item.value" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="操作步骤" prop="stepCode" required :rules="{ required: true, message: '请选择操作步骤' }">
          <el-select v-model="formData.stepCode" placeholder="请选择操作步骤">
            <el-option v-for="item in stepTypeEnumRef" :key="item.value" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="!abandonedBidFlag"
          label="评审项目节点"
          prop="itemId"
          required
          :rules="{ required: true, message: '请选择评审项目节点' }"
        >
          <el-select v-model="formData.itemId" placeholder="请选择评审项目节点">
            <el-option v-for="item in itemListRef" :key="item.value" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="情况记录" prop="comment">
          <el-input v-model="formData.comment" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
    </ClBar>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emit('atClose')">取消</el-button>
        <el-button type="primary" @click="onSure">确定</el-button>
      </span>
    </template>
  </ClDialog>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { getItemName, getSupplierNames } from './data'
import { supplierInfoList } from '@/api/electronic-bid-file'
import { getListByProjectId } from '@/api/project-scoring-standard-configuration'
import { useEnumeration } from '@/store/useEnumeration'
import { recordInfoDetail, recordInfoSaveUpdate } from '@/api/review-record'
const { getEnumValue, getStepTypeEnum } = useEnumeration()
const formData = ref({
  supplierIds: null,
  stepCode: undefined,
  itemId: undefined,
  comment: ''
})

const refForm = ref()
const onSure = () => {
  refForm.value.validate((valid) => {
    if (valid) {
      const cb = async (res) => {
        if (abandonedBidFlag.value) {
          res['itemId'] = undefined
          res['itemName'] = undefined
          res['supplierIds'] = undefined
          res['supplierNames'] = undefined
        } else {
          res['supplierNames'] = getSupplierNames(supplierInfoListRef.value, res['supplierIds'])
          res['itemName'] = getItemName(itemListRef.value, res['itemId'])
        }
        res['stepName'] = getEnumValue(res['stepCode'], 'stepTypeEnum')
        const temp = { ...res, id: props.info.id }
        await recordInfoSaveUpdate(temp)
        emit('atClose', 'refresh-table')
      }
      cb(formData.value)
    }
  })
}
onMounted(() => {
  init()
})
// 废标的时候 项目名称和评审项目节点隐藏
const abandonedBidFlag = ref(false)
watch(
  formData.value,
  (val) => {
    abandonedBidFlag.value = val.stepCode === 'DEPRECATED'
  },
  {
    deep: true
  }
)
const loaded = ref(false)
const stepTypeEnumRef = ref<any[]>([]) // 操作步骤
const itemListRef = ref<any[]>([]) // 评审项目节点
const supplierInfoListRef = ref<any[]>([]) // 供应商信息
const init = async () => {
  try {
    loaded.value = false
    const { rows }: any = await supplierInfoList({ unitConfirm: 1 })
    const itemList: any = await getListByProjectId()
    const stepTypeEnum: any = await getStepTypeEnum()

    // 操作步骤
    stepTypeEnumRef.value = stepTypeEnum
    // 获取供应商信息
    supplierInfoListRef.value = rows.map((item) => ({
      label: item.supplierName,
      value: item.supplierName,
      key: item.id
    }))
    // 评审项目节点
    itemListRef.value = itemList.map((item) => ({
      value: item.itemName,
      key: item.itemId
    }))

    if (props.info.id) {
      const res: any = await recordInfoDetail({ id: props.info.id })
      formData.value.supplierIds = res.supplierIds
      formData.value.stepCode = res.stepCode
      formData.value.itemId = res.itemId
      formData.value.comment = res.comment
    }
    loaded.value = true
  } catch (error) {
    loaded.value = true
    console.log('🐷 🐽 init 🐽 error👉👉👉', error)
  }
}

const getTitle = computed(() => {
  return props.type
})
const disabled = computed(() => {
  return props.info.isView
})

interface infoType {
  [props: string]: any
}
const props = defineProps<{ info: infoType; type: string }>()

const emit = defineEmits<{
  (e: 'atClose', str?: string | undefined): void
}>()
</script>

<style scoped lang="scss"></style>
